//
//  SLNetworkTool.m
//  SLToolKit
//
//  Created by CoderSLZeng on 2017/11/20.
//  

#import "SLNetworkTool.h"

@implementation SLNetworkTool

static SLNetworkTool *instance_;

+ (__kindof SLNetworkTool*)sl_sharedNetworkTool
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance_ = [[self alloc] initWithBaseURL:nil sessionConfiguration:NSURLSessionConfiguration.defaultSessionConfiguration];
        instance_.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:
                                                               @"application/json",
                                                               @"text/json",
                                                               @"text/javascript",
                                                               @"text/html",
                                                               nil];
    });
    return instance_;
}



- (void)sl_requestMethodType:(SLRequestType)methodType
                   urlString:(NSString *)urlString
                  parameters:(NSDictionary *)parameters
                    finished:(void (^)(NSDictionary *result, NSError *error))finished
{
    // 1.定义成功的回调
    void (^successCallBack)(NSURLSessionDataTask *task, id result) = ^(NSURLSessionDataTask *task, id result) {
        finished(result, nil);
    };
    
    // 2.定义失败的回调
    void (^failureCallBack)(NSURLSessionDataTask *task, NSError *error) = ^(NSURLSessionDataTask *task, NSError *error) {
        finished(nil, error);
    };
    
    // 3.发送网络请求
    if (methodType == SLRequestTypeGET) {
        [self GET:urlString
       parameters:parameters
         progress:nil
          success:successCallBack
          failure:failureCallBack];
    } else {
        [self POST:urlString
        parameters:parameters
          progress:nil
           success:successCallBack
           failure:failureCallBack];
    }
}


@end
